<template>
  <div class="params">
    <!-- 1. 目录位置 -->
    <div class="nav">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/params' }"
          >规格参数</el-breadcrumb-item
        >
        <el-breadcrumb-item>规格包装</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 2. 搜索框 -->
    <div class="header">
      <el-input v-model="input" @change="searchIpu" />
      <el-button type="primary">查看</el-button>
      <el-button type="primary" @click="showParams">添加</el-button>
    </div>

    <!-- 3. 表格 -->
    <el-table :data="tableData" class="my-table" border>
      <el-table-column prop="itemCatId" label="规格参数ID" width="120">
      </el-table-column>
      <el-table-column prop="id" label="类目ID" width="120"> </el-table-column>
      <el-table-column prop="specsName" label="规格名称" width="120">
      </el-table-column>
      <el-table-column prop="paramData" label="规格参数" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 4. 分页 -->
    <MyPagination
      :total="total"
      :pageSize="pageSize"
      @changePages="changePage"
    />
    <!-- 5. 弹框 -->
    <ParamsDialog ref="dialog" />
  </div>
</template>

<script>
import MyPagination from "../../../components/MyPagination.vue";
import ParamsDialog from "./ParamsDialog.vue";
export default {
  name: "ParamsPacking",
  components: { MyPagination, ParamsDialog },
  data() {
    return {
      input: "",
      tableData: [],
      total: 10,
      type:true,
      pageSize: 1,
    };
  },
  methods: {
    // 规格参数删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .deletaParams({
              id: row.id,
            })
            .then((res) => {
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                // 更新视图
                this.getParamasPage(1);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getParamasPage(page) {
      this.$api.getParamsInfo({ page }).then((res) => {
        if (res.data.status === 200) {
          this.tableData = res.data.data;
          // 获取分页
          this.total = res.data.total;
          this.pageSize = res.data.pageSize;
        }
      });
    },
    /**
     * 点击分页-切换
     */
    changePage(num) {
       if (this.type == true) {
        this.getParamasPage(num);
      } else {
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    // 显示弹框添加
    showParams() {
      this.$refs.dialog.dialogVisible = true;
    },
    // 搜索规格参数
    searchIpu(val){
       if (!val) {
        this.getParamasPage(1);
        this.currentPage = 1;
        this.type = true;
        return;
      }
      this.$api.searchParams({
        search:val
      }).then((res) => {
        console.log(res.data)
          this.currentPage = 1;
          if (res.data.status === 200) {
            this.list = res.data.result;
            this.total = res.data.result.length;
            this.pageSize = 3;
            this.tableData = res.data.result.slice(0, 3);
            this.type = false;
          } else {
            this.tableData = [];
            this.total = 1;
            this.pageSize = 1;
            this.type = true;
          }
        });
    },

  },
  created() {
    this.getParamasPage(1);
  },
};
</script>

<style lang="less" scoped>
.params {
  margin: 10px;
}
.nav {
  padding: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
}
.my-table {
  margin: 10px auto;
}
</style>
